#include "stdafx.h"
#include "ConfigurationLoader.h"
#include "GlobalInfoInstance.h"

CConfigurationLoader::CConfigurationLoader(void)
{
	m_ptr_xml_instance = new CXMLInstance("config.xml");
}


CConfigurationLoader::~CConfigurationLoader(void)
{
	if (m_ptr_xml_instance)
		delete m_ptr_xml_instance;
}

void CConfigurationLoader::LoadAllConfigurationInfos()
{
	LoadSignInfo();
	LoadSupportedInfo();
	LoadInputDevicesInfo();
}

BOOL CConfigurationLoader::LoadSignInfo()
{
	char str_keyvalue[32] = {0};
	sprintf(str_keyvalue, "CayMay/sign-info");
	xmlNodePtr xnp_sign_info = m_ptr_xml_instance->FindXmlNode(str_keyvalue);

	while (xnp_sign_info)
	{
		if (xnp_sign_info->type != XML_ELEMENT_NODE)
		{
			xnp_sign_info = xnp_sign_info->next;
			continue;
		}

		if (!strcmp((char*)xnp_sign_info->name, "server"))
		{
			if (xnp_sign_info->children && xnp_sign_info->children->content)
			{
				sprintf(CGlobalInfoInstance::GetInstance()->g_str_mgm_server_name, "%s", (char*)xnp_sign_info->children->content);
			}
		}
		else if (!strcmp((char*)xnp_sign_info->name, "username"))
		{
			if (xnp_sign_info->children && xnp_sign_info->children->content)
			{
				sprintf(CGlobalInfoInstance::GetInstance()->g_str_mgm_server_user, "%s", (char*)xnp_sign_info->children->content);
			}
		}
		else if (!strcmp((char*)xnp_sign_info->name, "password"))
		{
			if (xnp_sign_info->children && xnp_sign_info->children->content)
			{
				sprintf(CGlobalInfoInstance::GetInstance()->g_str_mgm_server_pwd, "%s", (char*)xnp_sign_info->children->content);
			}
		}
			
		xnp_sign_info = xnp_sign_info->next;
	}

	return TRUE;
}

BOOL CConfigurationLoader::LoadSupportedInfo()
{
	BOOL bln_ret_value = LoadSupportedIpCamerasInfo();
	return bln_ret_value;
}

BOOL CConfigurationLoader::LoadSupportedIpCamerasInfo()
{

	return TRUE;
}

BOOL CConfigurationLoader::LoadInputDevicesInfo()
{
	char str_keyvalue[32] = {0};
	sprintf(str_keyvalue, "CayMay/video-inputs");
	xmlNodePtr xnp_video_inputs = m_ptr_xml_instance->FindXmlNode(str_keyvalue);

	//xnp_video_input = xnp_video_input->children;
	while (xnp_video_inputs)
	{
		if (xnp_video_inputs->type != XML_ELEMENT_NODE)
		{
			xnp_video_inputs = xnp_video_inputs->next;
			continue;
		}

		if (!strcmp((char*)xnp_video_inputs->name, "video-input"))
		{

			INPUT_DEVICE* ptr_new_input_device = new INPUT_DEVICE; 

			xmlNodePtr xnp_video_input = xnp_video_inputs->children;
			while (xnp_video_input)
			{
				if (xnp_video_input->type != XML_ELEMENT_NODE)
				{
					xnp_video_input = xnp_video_input->next;
					continue;
				}
				if (!(xnp_video_input->children && xnp_video_input->children->content))
				{
					xnp_video_input = xnp_video_input->next;
					continue;
				}

				if (!strcmp((char*)xnp_video_input->name, "type"))
				{
					ptr_new_input_device->type = atoi((char*)xnp_video_input->children->content);
				}
				else if (!strcmp((char*)xnp_video_input->name, "name"))
				{
					sprintf(ptr_new_input_device->name, "%s", (char*)xnp_video_input->children->content);
				}
				else if (!strcmp((char*)xnp_video_input->name, "self-info"))
				{
					if (ptr_new_input_device->type == STREAM_INPUT_WEBCAM)
					{
						ptr_new_input_device->self_info = LoadInputDeviceWebCam(xnp_video_input->children);
					}
					else if (ptr_new_input_device->type == STREAM_INPUT_IPCAMERA)
					{
						ptr_new_input_device->self_info = LoadInputDeviceIpCamera(xnp_video_input->children);
					}
				}

				xnp_video_input = xnp_video_input->next;
			}

			CGlobalInfoInstance::GetInstance()->g_ary_input_devices.Add(ptr_new_input_device);
		}

		xnp_video_inputs = xnp_video_inputs->next;
	}
	
	return TRUE;
}

void* CConfigurationLoader::LoadInputDeviceWebCam(xmlNodePtr xnp_self_info)
{
	if (xnp_self_info == NULL)
		return NULL;

	DEVICE_WEBCAMERA* ptr_self_info = new DEVICE_WEBCAMERA;
	while (xnp_self_info)
	{
		if (xnp_self_info->type != XML_ELEMENT_NODE)
		{
			xnp_self_info = xnp_self_info->next;
			continue;
		}
		if (!(xnp_self_info->children && xnp_self_info->children->content))
		{
			xnp_self_info = xnp_self_info->next;
			continue;
		}

		if (!strcmp((char*)xnp_self_info->name, "name"))
		{
			sprintf(ptr_self_info->name, "%s", (char*)xnp_self_info->children->content);
		}
		else if (!strcmp((char*)xnp_self_info->name, "version"))
		{
			sprintf(ptr_self_info->version, "%s", (char*)xnp_self_info->children->content);
		}

		xnp_self_info = xnp_self_info->next;
	}

	return ptr_self_info;
}

void* CConfigurationLoader::LoadInputDeviceIpCamera(xmlNodePtr xnp_self_info)
{
	if (xnp_self_info == NULL)
		return NULL;

	DEVICE_IPCAMERA* ptr_self_info = new DEVICE_IPCAMERA;
	while (xnp_self_info)
	{
		if (xnp_self_info->type != XML_ELEMENT_NODE)
		{
			xnp_self_info = xnp_self_info->next;
			continue;
		}
		if (!(xnp_self_info->children && xnp_self_info->children->content))
		{
			xnp_self_info = xnp_self_info->next;
			continue;
		}

		if (!strcmp((char*)xnp_self_info->name, "address"))
		{
			sprintf(ptr_self_info->address, "%s", (char*)xnp_self_info->children->content);
		}
		else if (!strcmp((char*)xnp_self_info->name, "id"))
		{
			sprintf(ptr_self_info->id, "%s", (char*)xnp_self_info->children->content);
		}
		else if (!strcmp((char*)xnp_self_info->name, "password"))
		{
			sprintf(ptr_self_info->password, "%s", (char*)xnp_self_info->children->content);
		}
		else if (!strcmp((char*)xnp_self_info->name, "model"))
		{
			ptr_self_info->model = atoi((char*)xnp_self_info->children->content);
		}

		xnp_self_info = xnp_self_info->next;
	}

	return ptr_self_info;
}
